from modbus_rtu_client import ModbusRTUClient

DATA_MAP = {
    "baudrate": {
        1: '9600',
        2: '19200',
        3: '115200',
        4: '460800',
    },
    'switch': {
        0: '关闭',
        1: '打开',
    },
    'denoising_level':{
        0: '高灵敏模式',
        1: '高亢噪模式',
    },
    'show_noise':{
        0: '不显示噪声',
        1: '显示噪声',
    },
    'threshold_switch':{
        0: '关闭(0)',
    },
    'aa_alarm_level': {
        0: '干扰(0)',
        1: '放电(1)',
    },
}


def process_high_low_value(data):
    h_value = hex(data[0])
    l_value = hex(data[1])
    value = f"{h_value[2:]}{l_value[2:]}"
    return value

def int_to_int16(value):
    """int转有符号16进制"""
    if value >= 32768:
        value -= 65536
    return value


def process_map_data(data, map_name='switch'):
    if map_name:
        return DATA_MAP[map_name].get(data, data)

    else:
        return data



def get_version(modbus_client):
    result = modbus_client.read_input_registers(0x31, 2)
    print(result)
    if result['success']:
        return process_high_low_value(result['data'])
    else:
        return '--'


def get_device_params(modbus_client):
    parameters = {}
    result = modbus_client.read_input_registers(0x0051, 76)
    print(result)
    if result['success']:
        data = result['data']
        device_code = process_high_low_value(data[-2:])
        slave_id = data[0]
        baudrate = process_map_data(data[1], 'baudrate')
        pf_trigger_switch = process_map_data(data[3])
        phase_offset = int_to_int16(data[4])
        tev_abs_threshold = data[5]
        uhf_abs_threshold = data[6]
        aa_abs_threshold = data[7]
        tev_amplitude_alarm_threshold = data[8]
        tev_frequency_alarm_threshold = data[9]
        uhf_amplitude_alarm_threshold = data[10]
        uhf_frequency_alarm_threshold = data[11]
        aa_amplitude_alarm_threshold = data[12]
        aa_frequency_alarm_threshold = data[13]
        polling_interval = data[32]
        tev_m_value = data[33]
        tev_base_frequency = data[34]
        tev_db_value = data[35]
        tev_sensor_params = data[36]
        tev_difference_config = data[37]
        tev_additional_base = int_to_int16(data[38])
        uhf_m_value = data[44]
        uhf_base_frequency = data[45]
        uhf_db_value = data[46]
        uhf_sensor_params = data[47]
        uhf_difference_config = data[48]
        uhf_additional_base = int_to_int16(data[49])
        aa_m_value = data[54]
        aa_base_frequency = data[55]
        aa_db_value = data[56]
        aa_sensor_params = data[57]
        aa_difference_config = data[58]
        aa_additional_base = int_to_int16(data[59])
        denoising_level = process_map_data(data[65], 'denoising_level')
        show_noise = process_map_data(data[66], 'show_noise')
        tev_auto_threshold_toggle = process_map_data(data[67], 'threshold_switch')
        uhf_auto_threshold_toggle = process_map_data(data[68], 'threshold_switch')
        aa_auto_threshold_toggle = process_map_data(data[69], 'threshold_switch')
        tev_waveform_filter_enabled = process_map_data(data[70])
        uhf_waveform_filter_enabled = process_map_data(data[71])
        aa_waveform_filter_enabled = process_map_data(data[72])

        parameters = {
            'device_code': device_code,
            'slave_id': slave_id,
            'baudrate': baudrate,
            'pf_trigger_switch': pf_trigger_switch,
            'phase_offset': phase_offset,
            'tev_abs_threshold': tev_abs_threshold,
            'uhf_abs_threshold': uhf_abs_threshold,
            'aa_abs_threshold': aa_abs_threshold,
            'tev_amplitude_alarm_threshold': tev_amplitude_alarm_threshold,
            'tev_frequency_alarm_threshold': tev_frequency_alarm_threshold,
            'uhf_amplitude_alarm_threshold': uhf_amplitude_alarm_threshold,
            'uhf_frequency_alarm_threshold': uhf_frequency_alarm_threshold,
            'aa_amplitude_alarm_threshold': aa_amplitude_alarm_threshold,
            'aa_frequency_alarm_threshold': aa_frequency_alarm_threshold,
            'polling_interval': polling_interval,
            'tev_m_value': tev_m_value,
            'tev_base_frequency': tev_base_frequency,
            'tev_db_value': tev_db_value,
            'tev_sensor_params': tev_sensor_params,
            'tev_difference_config': tev_difference_config,
            'tev_additional_base': tev_additional_base,
            'uhf_m_value': uhf_m_value,
            'uhf_base_frequency': uhf_base_frequency,
            'uhf_db_value': uhf_db_value,
            'uhf_sensor_params': uhf_sensor_params,
            'uhf_difference_config': uhf_difference_config,
            'uhf_additional_base': uhf_additional_base,
            'aa_m_value': aa_m_value,
            'aa_base_frequency': aa_base_frequency,
            'aa_db_value': aa_db_value,
            'aa_sensor_params': aa_sensor_params,
            'aa_difference_config': aa_difference_config,
            'aa_additional_base': aa_additional_base,
            'denoising_level': denoising_level,
            'show_noise': show_noise,
            'tev_auto_threshold_toggle': tev_auto_threshold_toggle,
            'uhf_auto_threshold_toggle': uhf_auto_threshold_toggle,
            'aa_auto_threshold_toggle': aa_auto_threshold_toggle,
            'tev_waveform_filter_enabled': tev_waveform_filter_enabled,
            'uhf_waveform_filter_enabled': uhf_waveform_filter_enabled,
            'aa_waveform_filter_enabled': aa_waveform_filter_enabled,
            'soft_version': get_version(modbus_client),
        }

    return parameters


def get_device_data(modbus_client):
    device_data = {}
    result = modbus_client.read_input_registers(0x01, 53)
    print(result)
    if result['success']:
        data = result['data']
        device_data = {
            'tev_amplitude': data[0]/10,
            'tev_frequency': data[1],
            'tev_noise_level': data[2]/10,
            'tev_noise_mean': data[3]/10,
            'tev_pulse_cycles': data[4],
            'tev_severity': data[5],
            'tev_50hz': data[6]/100,
            'tev_100hz': data[7]/100,
            'tev_fft_frequency': data[8],
            'tev_max_fft_frequency': data[9]/100,
            'uhf_amplitude': data[15],
            'uhf_frequency': data[16],
            'uhf_noise_level': data[17]/10,
            'uhf_avg_discharge_current': data[18],
            'uhf_pulse_cycles': data[19],
            'uhf_max_discharge_capacity': data[20] / 10,
            'uhf_min_discharge_capacity': data[21] / 10,
            'uhf_50hz': data[22]/100,
            'uhf_100hz': data[23]/100,
            'uhf_fft_frequency': data[24],
            'uhf_max_fft_frequency': data[25]/100,
            'aa_amplitude': data[31]/100,
            'aa_frequency': data[32],
            'aa_noise_level': data[33]/100,
            'aa_mean_value': data[34]/100,
            'aa_effective_value': data[35]/100,
            'aa_frequency_component1': data[36]/100,
            'aa_frequency_component2': data[37]/100,
            'aa_alarm_level': process_map_data(data[38], map_name='aa_alarm_level'),
            'aa_50hz': data[39]/100,
            'aa_100hz': data[40]/100,
            'aa_fft_frequency': data[41],
            'aa_max_fft_frequency': data[42]/100,
        }
    return device_data

if __name__ == '__main__':
    modbus_client = ModbusRTUClient(port='COM13', slave_address=1)
    # data = get_device_params(modbus_client)
    # data = get_version(modbus_client)
    data = get_device_data(modbus_client)
    print(data)
